package hot100;

import com.链表.练习题.ListNode;

public class g3_回文链表 {
    public boolean isPalindrome(ListNode head) {
        ListNode middle = middle(head);
        ListNode newHead = reverse(middle);
        while (newHead != null){
            if(newHead.val != head.val){
                return false;
            }
            newHead = newHead.next;
            head = head.next;
        }
        return true;
    }
    private ListNode reverse(ListNode head) {
        ListNode n1 = null;
        while (head != null){
            ListNode n2 = head.next;
            head.next = n1;
            n1 = head;
            head = n2;
        }
        return n1;
    }
    private ListNode middle(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
}
